/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : NpProcessHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Nov 27, 2008       Raghu Sarraff			       Created
 */
package net.lwf.handler.math.number;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.entity.QuestionInfo;
import net.lwf.number.util.NumberFormatter;
import net.lwf.number.util.NumberRounder;
import net.lwf.number.util.RandomNumberGenerator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;


public class NpProcessHandler extends ProcessHandler implements IProcessHandler {
	
	private int NUMBER_START_RANGE = 1;
	private int NUMBER_END_RANGE = 99999;
	
	private String grade;
	
	public NpProcessHandler(String grade) {
		this.grade = grade;
	}
	public DisplayFormatter getDisplayFormatter() {
		
		return null;
	}
	public String getQuestionDesc() {
		
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(false);
	}
	

	public QuestionInfo generateSingle(boolean isMultiple) {
		QuestionInfo qinfo = new QuestionInfo();
		int number = RandomNumberGenerator.generateInt(NUMBER_START_RANGE, NUMBER_END_RANGE);
		String sNumber = String.valueOf(number);

		Map<String, String> mapData = NumberRounder.numberPosition(number);
		int[] choices = RandomNumberGenerator.generateInts(1, sNumber.length(), sNumber.length());
		int pickQ = RandomNumberGenerator.generate(1,sNumber.length());
		int key = choices[pickQ-1];
		
		qinfo.setAnswer(mapData.get(String.valueOf(key)));
		qinfo.setQuestion(NumberFormatter.format(sNumber));
		qinfo.setPlaceHolder(this.generateQuestionDesc(key)); //For instructional text on the UI
		
		if(!isMultiple) {
			//Set radio answers
			qinfo.setRadioAnswerChoices(this.getRadioAnswers(mapData));			
		}

		
		return qinfo;
	}
	
	private List<RadioData> getRadioAnswers(Map<String, String> mapAnswers) {
		List<RadioData> rdata = new ArrayList<RadioData>();
		List<String> lstanswers = new ArrayList<String>(mapAnswers.values());
		for(String answer: lstanswers) {
			RadioData data = new RadioData(answer, answer);
			rdata.add(data);
		}
		return rdata;
	}
	
	private String generateQuestionDesc(int key) {
		
		String question = "What is ";

		if(key == 1) {
			question += "in ones position?";
		}
		else if(key == 2) {
			question += "in tens position?";
		}
		else if(key == 3) {
			question += "in hundreds position?";
		}
		else if(key == 4) {
			question += "in thousands position?";
		}
		else if(key == 5) {
			question += "in ten thousands position?";
		}
		else if(key == 6) {
			question += "in hundred thousands position?";
		}
		else {
			question += "in millionth position?";
		}
		
		return question;
		
	}
	
	@Override
	public List<QuestionInfo> generateMultiple() {
		List<QuestionInfo> questions = new ArrayList<QuestionInfo>();
		int nQuestions = getRows() * getColumns();
		for(int i=0; i<nQuestions; i++) {
			QuestionInfo question = generateSingle(true);
			question.setQuestionNumber(i+1);
			questions.add(question);
		}
		return questions;
	}
	@Override
	public void getContent(Document document) throws DocumentException {
		
		
	}
	@Override
	public int getRows() {
		
		return 0;
	}
	@Override
	public int getColumns() {
		
		return 0;
	}
	@Override
	public boolean isPortrait() {
		
		return true;
	}
	@Override
	public String getPdfInstructionalText() {
		
		return null;
	}
	@Override
	public String getPdfNote() {
		
		return null;
	}	
}

